package com.happy3w.ssp.schoolmatehome.component.image.type.image;

import com.happy3w.ssp.schoolmatehome.exception.MessageException;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

@RequiredArgsConstructor
@Getter
public class ResImage {
    private final String path;

    private BufferedImage image;
    public BufferedImage getImage() {
        if (image != null) {
            return image;
        }
        try(InputStream imgStream = ResImage.class.getResourceAsStream(path)) {
            if (imgStream == null) {
                throw new MessageException("No img:" + path);
            }
            image = ImageIO.read(imgStream);
        } catch (IOException e) {
            throw new MessageException("Failed to load image", e);
        }
        return image;
    }
}
